﻿using FashionWebsite.Models;
using FashionWebsite.Models.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FashionWebsite.Controllers
{
    public class CategoryManagerController : Controller
    {
        //
        // GET: /CategoryManager/
        ShopOnlineDataContext data = new ShopOnlineDataContext();

        public ActionResult Index()
        {
            var categories = from c in data.Categories
                             select c;
            var viewModel = new DalCategory
            {
                category = categories.ToList(),
            };
            return View(viewModel);
        }
      
        public ActionResult Create()
        {
            
            //ViewData["listProductCatelogy"] = list;
            return View();

        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "CategoryId")] Category category)
        {
            //map data back to dictionary
            try
            {

                data.Categories.InsertOnSubmit(category); 
                data.SubmitChanges();
                return View();
            }
            catch (Exception)
            {
                return View();
            }

        }

        public ActionResult Edit(int id)
        {
            var categorys = data.Categories.SingleOrDefault(x => x.CategoryId == id);

            return View(categorys);


        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id,  Category category )
        {
            var categorys = data.Categories.SingleOrDefault(x => x.CategoryId.ToString() == id);

            UpdateModel(categorys);

            data.SubmitChanges();
            return View();
        }

        public ActionResult Delete(string id)
        {
            var category = data.Categories.SingleOrDefault(x => x.CategoryId.ToString() == id);

            data.Categories.DeleteOnSubmit(category);

            data.SubmitChanges();
            return RedirectToAction("Index");
        }

    }
}